package com.tul.exercise.base.java8.stream.base;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DishTest
 * @Description 测试stram使用
 * @Author tul 2018/12/22
 * @Version 1.0
 **/
public class DishTest {
    public static void main(String[] args) {

        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french", false, 530, Dish.Type.OTHER),
                new Dish("rice", false, 350, Dish.Type.OTHER),
                new Dish("season fruit", false, 120, Dish.Type.OTHER),
                new Dish("pizza", false, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.OTHER),
                new Dish("salmon", false, 450, Dish.Type.OTHER)
        );

        // 串行运行
        List<String> lowCaloricDishNames = menu.stream().filter(d -> {
            System.out.println("filtering:" + d.getName());
            return d.getCalories() < 400;
        })
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(Collectors.toList());
        System.out.println(lowCaloricDishNames);

        // 并行运行
        List<String> lowCaloricDishNames2 = menu.parallelStream().filter(d -> d.getCalories() < 400)
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(Collectors.toList());
        System.out.println(lowCaloricDishNames);

        // 计数
        long conut = menu.stream().filter(d -> d.getCalories() < 400).distinct().count();
        System.out.println("低卡路里个数：" + conut);

    }
}
